package com.anxin.act.listener;

import com.alibaba.fastjson.JSONArray;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.sys.system.entity.SysUser;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.impl.persistence.entity.CommentEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.task.Comment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 自建流程监听器
 */
@Configuration
public class SelfBuiltListener implements ExecutionListener,TaskListener {

	private Logger logger = LoggerFactory.getLogger("sys-info");
	private static final long serialVersionUID = 1L;
	@Autowired
	private RuntimeService runtimeService;

	@Override
	public void notify(DelegateTask delegateTask) {

	}

	@Override
	public void notify(DelegateExecution execution) throws Exception {

	}

	/**
	 * 获取审批人
	 * @param execution
	 * @return
	 */
	public List<Object> getApprover(DelegateExecution execution){
		List<Object> approverList = new ArrayList<>();
		String selfBuiltAssigneesRemaining = runtimeService.getVariable(execution.getId(), "selfBuiltAssigneesRemaining").toString();
		JSONArray selfBuiltAssigneesRemainingArray = JSONArray.parseArray(selfBuiltAssigneesRemaining);
		if (selfBuiltAssigneesRemainingArray.size() > 0 ){
			approverList = Arrays.asList(selfBuiltAssigneesRemainingArray.getJSONArray(0).toArray());
			// 从剩余审批人中删除当前节点审批人
			//selfBuiltAssigneesRemainingArray.remove(0);
			//runtimeService.setVariable(execution.getId(),"selfBuiltAssigneesRemaining",selfBuiltAssigneesRemainingArray.toJSONString());
		}
		return approverList;
	}

	/**
	 * 删除掉第一个审批人
	 * @param execution
	 */
	public void removeApprover(DelegateExecution execution){
		String selfBuiltAssigneesRemaining = runtimeService.getVariable(execution.getId(), "selfBuiltAssigneesRemaining").toString();
		JSONArray selfBuiltAssigneesRemainingArray = JSONArray.parseArray(selfBuiltAssigneesRemaining);
		if (selfBuiltAssigneesRemainingArray.size() > 0 ){
			// 从剩余审批人中删除当前节点审批人
			selfBuiltAssigneesRemainingArray.remove(0);
			runtimeService.setVariable(execution.getId(),"selfBuiltAssigneesRemaining",selfBuiltAssigneesRemainingArray.toJSONString());
		}
	}

	/**
	 * 重置审批人
	 * @param execution
	 */
	public void resetApprover(DelegateExecution execution){
		runtimeService.setVariable(execution.getId(),"selfBuiltAssigneesRemaining",runtimeService.getVariable(execution.getId(), "selfBuiltAssigneesAll"));
	}

	/**
	 * 获取下一审批人长度
	 * @param execution
	 * @return
	 */
	public int getNextApproverSize(DelegateExecution execution){
		String selfBuiltAssigneesRemaining = runtimeService.getVariable(execution.getId(), "selfBuiltAssigneesRemaining").toString();
		JSONArray selfBuiltAssigneesRemainingArray = JSONArray.parseArray(selfBuiltAssigneesRemaining);
		// System.err.println(selfBuiltAssigneesRemainingArray.size() == 0);
		if (selfBuiltAssigneesRemainingArray.size() == 0){
			return -1;
		}
		return selfBuiltAssigneesRemainingArray.getJSONArray(0).size();
	}

}
